
import {
  request
} from "../../utils/request.js";
import regeneratorRuntime from '../../utils/runtime';


Component({
  options: {
    addGlobalClass: true,
  },
  data: {},
  properties: {
    // 内容ID
    contentId:{
      type:String,
      value:'0'
    },
    // 内容类型
    contentType:{
      type:String,
      value:'author'
    },
    // 具体内容
    content:{
      type:String,
      value:'133'
    },
    // 收藏标记
    collectFlag:{
      type:String,
      value:'N'
    },
    url:{
      type:String,
      value:''
    }

  },
  methods: {

    /**
     * 取消收藏
     */
    async handleCancelCollect(){

      // 判断是否登录，如果没有登录，给出登录提示
      if(!this.validLogin()){
        return;
      }

      let openId = wx.getStorageSync("openId");
      const res = await request({
        url: "/collect/cancelCollect?contentId=" + this.data.contentId 
        + "&contentType=" + this.data.contentType
        + "&openId=" + openId,
        method: "get"
      });
      if(res.code != 0){
        return;
      }
      this.setData({
        collectFlag:'N'
      });
    },
    /**
     * 收藏
     */
    async handleCollect(){

      // 判断是否登录，如果没有登录，给出登录提示
      if(!this.validLogin()){
        return;
      }

      let openId = wx.getStorageSync("openId");
      const res = await request({
        url: "/collect/collectContent?contentId=" + this.data.contentId 
        + "&contentType=" + this.data.contentType
        + "&openId=" + openId,
        method: "get"
      });
      if(res.code != 0){
        return;
      }
      wx.showToast({
        title: "收藏成功",
        duration: 1700
      });
      this.setData({
        collectFlag:'Y'
      });

    },

    /**
     * 复制 
     */
    handleCopy(){
      wx.setClipboardData({
        data: this.data.content,
      })
      wx.showToast({
        title: "复制成功",
        duration: 1700
      });
    },

    /**
     * 验证是否登录
     */
    validLogin(){
      let openId = wx.getStorageSync("openId");
      if(!openId){
        wx.showToast({
          title: "请登录后进行操作",
          duration: 1700
        });
        return false;
      }
      return true;
    },

  }
})